﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;

	public class UserMessageSingleReader : IReader<UserMessage>
	{
		public void Populate(SqlDataReader reader, UserMessage entity)
		{
			entity.Id = reader.GetInt32(0);
			entity.ParentId = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);
			entity.Title = reader.GetString(2);
			entity.Slug = reader.GetString(3);
			entity.Body = reader.GetString(4);
			entity.BodyParsed = reader.GetString(5);
			entity.SenderActive = reader.GetBoolean(6);
			entity.ReceiverActive = reader.GetBoolean(7);
			entity.ReceiverRead = reader.GetBoolean(8);
			entity.ReceiverReplied = reader.GetBoolean(9);
			entity.ReceiverForwarded = reader.GetBoolean(10);

			if (!reader.IsDBNull(11))
			{
				entity.Read = reader.GetDateTime(11);
			}

			entity.Created = reader.GetDateTime(12);
			entity.Updated = reader.GetDateTime(13);

			entity.Sender = new User
        	{
				Id = reader.GetInt32(14),
        		Name = reader.GetString(15),
        		Slug = reader.GetString(16),
				Reputation = reader.GetInt32(17),
        		Profile = new UserProfile { Title = reader.GetString(18) }
        	};

			if (!reader.IsDBNull(19))
			{
				entity.Sender.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(19),
					FileDiskName = reader.GetString(20),
					FilePath = reader.GetString(21),
					FileType = reader.GetString(22)
				};
			}

			entity.Receiver = new User
			{
				Id = reader.GetInt32(23),
				Name = reader.GetString(24),
				Slug = reader.GetString(25),
				Reputation = reader.GetInt32(26),
				Profile = new UserProfile { Title = reader.GetString(27) }
			};

			if (!reader.IsDBNull(28))
			{
				entity.Receiver.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(28),
					FileDiskName = reader.GetString(29),
					FilePath = reader.GetString(30),
					FileType = reader.GetString(31)
				};
			}

			if (!reader.IsDBNull(32))
			{
				entity.Folder = new UserMessageFolder
				{
					Id = reader.GetInt32(32),
					Title = reader.GetString(34), 
					Slug = reader.GetString(35)
				};
			}
		}
	}
}